/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package pl.polidea.lab.microlog4e.views;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

import pl.polidea.lab.microlog4e.J2meDevice;
import pl.polidea.lab.microlog4e.MicrologPlugin;
import pl.polidea.lab.microlog4e.ddmlib.IDevice;
import pl.polidea.lab.microlog4e.ddmuilib.DevicePanel;
import pl.polidea.lab.microlog4e.ddmuilib.ScreenShotDialog;
import pl.polidea.lab.microlog4e.ddmuilib.DevicePanel.IUiSelectionListener;



public class DeviceView extends ViewPart implements IUiSelectionListener {


    public static final String ID = "com.android.ide.eclipse.ddms.views.DeviceView"; //$NON-NLS-1$

    private static DeviceView sThis;

    private DevicePanel mDevicePanel;

    private Action mCaptureAction;
    private Action mUpdateHeapAction;
    private Action mGcAction;
    private Action mKillMidletAction;


    public DeviceView() {
        // the view is declared with allowMultiple="false" so we
        // can safely do this.
        sThis = this;
    }

    public static DeviceView getInstance() {
        return sThis;
    }



    @Override
    public void createPartControl(Composite parent) {

        mDevicePanel = new DevicePanel(MicrologPlugin.getImageLoader());
        mDevicePanel.createPanel(parent);
        mDevicePanel.addSelectionListener(this);

        MicrologPlugin plugin = MicrologPlugin.getDefault();
        mDevicePanel.addSelectionListener(plugin);
        plugin.setListeningState(true);

        mCaptureAction = new Action("Screen Capture") {
            @Override
            public void run() {
            	
                ScreenShotDialog dlg = new ScreenShotDialog( MicrologPlugin.getDisplay().getActiveShell());
                dlg.open(mDevicePanel.getSelectedDevice());
            }
        };
        mCaptureAction.setToolTipText("Screen Capture");
        mCaptureAction.setImageDescriptor(MicrologPlugin.getImageLoader().loadDescriptor("capture.png")); //$NON-NLS-1$

        mKillMidletAction = new Action() {
            @Override
            public void run() {
                //mDeviceList.killSelectedClient();
            }
        };

        mKillMidletAction.setText("Stop Midlet");
        mKillMidletAction.setToolTipText("Stop Midlet");
        mKillMidletAction.setImageDescriptor(MicrologPlugin.getImageLoader().loadDescriptor(DevicePanel.ICON_HALT));

        mGcAction = new Action() {
            @Override
            public void run() {
                //mDeviceList.forceGcOnSelectedClient();
            }
        };

        mGcAction.setText("Cause GC");
        mGcAction.setToolTipText("Cause GC");
        mGcAction.setImageDescriptor(MicrologPlugin.getImageLoader().loadDescriptor(DevicePanel.ICON_GC));

        mUpdateHeapAction = new Action("Update Heap", IAction.AS_CHECK_BOX) {
            @Override
            public void run() {
                //boolean enable = mUpdateHeapAction.isChecked();
            }
        };
        mUpdateHeapAction.setToolTipText("Update Heap");
        mUpdateHeapAction.setImageDescriptor(MicrologPlugin.getImageLoader().loadDescriptor(DevicePanel.ICON_HEAP));
        placeActions();
    }

    @Override
    public void setFocus() {
        mDevicePanel.setFocus();
    }

    /**
     * Sent when a new {@link IDevice} and {@link Client} are selected.
     * @param selectedDevice the selected device. If null, no devices are selected.
     * @param selectedClient The selected client. If null, no clients are selected.
     */
    public void selectionChanged(J2meDevice selectedDevice) {
        // update the buttons
        doSelectionChanged(selectedDevice);
    }



    private void doSelectionChanged(J2meDevice selectedDevice) {
        mCaptureAction.setEnabled(selectedDevice != null);
    }

    /**
     * Place the actions in the ui.
     */
    private final void placeActions() {
        IActionBars actionBars = getViewSite().getActionBars();

        // and then in the toolbar
        IToolBarManager toolBarManager = actionBars.getToolBarManager();
        toolBarManager.removeAll();

        toolBarManager.add(mUpdateHeapAction);
        toolBarManager.add(mGcAction);
        toolBarManager.add(new Separator());
        toolBarManager.add(mKillMidletAction);
        toolBarManager.add(new Separator());
        toolBarManager.add(mCaptureAction);
        toolBarManager.add(new Separator());
    }



}
